#include "Mario.h"
#include "ResourceManager.h"
#include "Texture.h"

Mario::Mario(float x, float y,int state)
{
	//_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Mario), 150, 0);
	_spriteSmall = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Mario_Small), 150, 0);
	_spriteBig = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Mario_Big), 150, 0);
	_spriteBullet = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Mario_Bullet), 150, 0);
	_spriteLight = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Mario_Small_Light), 150, 0);
	_sprite = _spriteSmall;
	_turnLeft = false;
	_isJumping = false;
	

	isGravity = true;
	isSmall = true;			//mario con nho
	isShoot = false;		//chua co bullet
	isHasStar = false;		//chua co sao ba dao
	isMoveDown = false;		//chua chuoi cong
	isGravity = true;		//co Trong luc

	_x = x;
	_y = y;
	_xPre = x;
	_yPre = y;

	

	_vx = 0;
	_vy = 0;

	_accelX = 0;
	_accelY = 0;

	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();

	_state = state;

	timeBullet = 0;
	timeTransferState = 0;
	countFrame = 0;

	bullet = NULL;
	bullet2 = NULL;
	

}


Mario::~Mario(void)
{
	//delete _texture;
	delete _sprite;
}


void Mario::Update(list<Object*>* obj_list, float time)
{
	switch (this->_state)
	{
	case MS_NORMAL:
		//Update Input
		

		this->Move(obj_list, time);

		if (bullet != NULL)
		{
			bullet->Update(obj_list, time);
		}
		if (bullet2 != NULL)
		{
			bullet2->Update(obj_list, time);
		} 
		break;

	case MS_DIE:
				
		_vy -= _accelY * time;
		_y += _vy * time + 1/2 * _accelY * time * time;

		if (_y <= 0)		///roi xuong ho, cuoi man hinh
		{
			changeState(MS_REMOVE);
			
		}
		break;

	case MS_SMALLTOBIG:
		if (_turnLeft == true)
		{
			_direction = 1;
		} 
		else
		{
			_direction = 2;
		}
		timeTransferState += time;
		if (timeTransferState > 40)
		{
			timeTransferState = 0;
			countFrame++;
			if (countFrame % 2 != 0)		//frame le
			{
				_y = _y + 30;
				_sprite = _spriteBig;
				if (_turnLeft == true)
				{
					_sprite->SetFrame(8, 9);
				}
				else
				{
					_sprite->SetFrame(1, 2);
				}
			}
			else
			{
				_y = _y - 30;
				_sprite = _spriteSmall;
				if (_turnLeft == true)
				{
					_sprite->SetFrame(10,11);
				} 
				else
				{
					_sprite->SetFrame(0,1);
				}
			}
			if (countFrame > 8)
			{
				//ChangeSprite(_spriteBig);
				_sprite = _spriteBig;
				changeState(MS_NORMAL);
			}
		}
		break;


	case MS_BIGTOSMALL:
		if (_turnLeft == true)
		{
			_direction = 1;
		} 
		else
		{
			_direction = 2;
		}
		timeTransferState += time;
		if (timeTransferState > 40)
		{
			timeTransferState = 0;
			countFrame++;
			if (countFrame % 2 != 0)		//frame le
			{
				_y = _y - 30;
				_sprite = _spriteSmall;
				if (_turnLeft == true)
				{
					_sprite->SetFrame(10,11);
				} 
				else
				{
					_sprite->SetFrame(0,1);
				}
			}
			else								//frame chan
			{
				_y = _y + 30;
				_sprite = _spriteBig;
				if (_turnLeft == true)
				{
					_sprite->SetFrame(8,9);
				} 
				else
				{
					_sprite->SetFrame(1,2);
				}

			}
			
			if (countFrame > 8)
			{				
				_sprite = _spriteSmall;
				changeState(MS_PROTECT);
			}
		}

		break;

	case MS_BULLETTOSMALL:
		if (_turnLeft == true)
		{
			_direction = 1;
		} 
		else
		{
			_direction = 2;
		}
		timeTransferState += time;
		if (timeTransferState > 40)
		{
			timeTransferState = 0;
			countFrame++;
			if (countFrame % 2 != 0)		//frame le
			{
				_y = _y - 30;
				_sprite = _spriteSmall;
				if (_turnLeft == true)
				{
					_sprite->SetFrame(10,11);
				} 
				else
				{
					_sprite->SetFrame(0,1);
				}
			}
			else								//frame chan
			{
				_y = _y + 30;
				_sprite = _spriteBullet;
				if (_turnLeft == true)
				{
					_sprite->SetFrame(8,9);
				} 
				else
				{
					_sprite->SetFrame(1,2);
				}

			}
			if (countFrame > 8)
			{
				_sprite = _spriteSmall;
				changeState(MS_PROTECT);
			}
		}
		
		break;

	case MS_BIGTOBULLET:
		if (_turnLeft == true)
		{
			_direction = 1;
		} 
		else
		{
			_direction = 2;
		}
		timeTransferState += time;
		if (timeTransferState > 40)
		{
			timeTransferState = 0;
			countFrame++;
			if (countFrame % 2 != 0)		//frame le
			{
				_sprite = _spriteBullet;
				if (_turnLeft == true)
				{
					_sprite->SetFrame(8,9);
				} 
				else
				{
					_sprite->SetFrame(1,2);
				}
			}
			else								//frame chan
			{
				_sprite = _spriteBig;
				if (_turnLeft == true)
				{
					_sprite->SetFrame(8,9);
				} 
				else
				{
					_sprite->SetFrame(1,2);
				}
			}
			if (countFrame > 8)
			{				
				_sprite = _spriteBullet;
				changeState(MS_NORMAL);
			}
		}
		break;

	case MS_PROTECT:
		

		if (_turnLeft == true)
		{
			_direction = 1;
		} 
		else
		{
			_direction = 2;
		}

		timeProtect += time;
		if (timeProtect > 40)
		{
			timeProtect = 0;
			countFrame++;
			if (countFrame % 2 != 0)		//frame le
			{
				_sprite = _spriteLight;

			}
			else								//frame chan
			{
				_sprite = _spriteSmall;

			}
			
			if (countFrame > 43)
			{
				_sprite = _spriteSmall;

				changeState(MS_NORMAL);
			}
		}

		break;

	case MS_HASSTAR:

		Move(obj_list, time);

		if (bullet != NULL)
		{
			bullet->Update(obj_list, time);
		}
		if (bullet2 != NULL)
		{
			bullet2->Update(obj_list, time);
		} 
		timeProtect += time;
		if (timeProtect > 40)
		{
			timeProtect = 0;
			countFrame++;
			if (countFrame % 2 != 0)		//frame le
			{
				if (isSmall == true)
				{
					_sprite = _spriteLight;
				} 
				else
				{
					_sprite = _spriteBig;
				}
			}
			else								//frame chan
			{
				if (isSmall == true)
				{
					_sprite = _spriteSmall;
				} 
				else
				{
					_sprite = _spriteBullet;
				}
			}
			if (countFrame > 243)
			{
				isHasStar = false;
				if (isSmall == true)
				{
					_sprite = _spriteSmall;
				} 
				else
				{
					if (isShoot == true)
					{
						_sprite = _spriteBullet;
					} 
					else
					{
						_sprite = _spriteBig;
					}
				}
				changeState(MS_NORMAL);
			}
		}
		break;

	}
		_sprite->Update(time);
}

void Mario::Render(int vpX, int vpY)
{
	_sprite->Render(_x, _y, vpX, vpY);
	if (bullet != NULL)
		{
			bullet->Render(vpX, vpY);
		}
		if (bullet2 != NULL)
		{
			bullet2->Render(vpX, vpY);
		} 
}

void Mario::Move(list<Object*>* obj_list, float time)
{
	timeBullet += time;
	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();

	Object *obj;
	list<Object*>::iterator i_obj;

	//Truc X
	_vx += _accelX * time;
	if (_vx > MARIO_VX_MAX)
	{
		_vx = MARIO_VX_MAX;
	} 
	else if (_vx < -MARIO_VX_MAX)
	{
		_vx = - MARIO_VX_MAX;
	}
	_x += _vx * time + 1.0f/2.0f * _accelX * time * time;

	//Truc Y
	if (isGravity == false)
	{
		_accelY = 0;
	}
	else
	{
		_accelY = MARIO_GRAVITY;
	}
	//_accelY = GRAVITY;	
	_vy -= _accelY * time;
	_y += _vy * time + 1.0f/2.0f * _accelY * time * time;


	/*
	*	Kiem tra va cham voi cac doi tuong tinh
	*/
	for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
	{
		obj = *i_obj;
		
		//Box box = this->getBox(time);
		Box box(this->_xPre,
			this->_yPre - this->_height, 
			_width, 
			_height, 
			_vx * time + 1.0f/2.0f * _accelX * time * time, 
			_vy * time  + 1.0f/2.0f * _accelY * time * time);

		//Box block = obj->getBox(time);
		Box block(obj->getPosX(),
			obj->getPosY() - obj->getHeight(),// - 0.11,
			obj->getWidth(),
			obj->getHeight(),
			obj->getVelocX() * time + 1.0f/2.0f * obj->getAccelX() * time * time, 
			obj->getVelocY() * time  + 1.0f/2.0f * obj->getAccelY() * time * time
			);

		Box boxphase = Collision::_GetSweptBroadphaseBox(box);
		if (Collision::_AABBCheck(boxphase, block))
		{
			float normalx = 0;
			float normaly = 0;
			float collision_time = Collision::_SweptAABB(box, block, normalx, normaly);
			if (0 < collision_time && collision_time < 1)
			{
				
				/*
				*	Xet Huong Cua Va Cham
				*/
				if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
				{
					if (obj->getIDObjType() == 12)	//koopa
					{						
						if (isHasStar == false)	//chua co an sao
						{
							if (obj->getObjState() == ES_STOP)
							{
								obj->changeState(ES_MOVE_SHELL_LEFT);	//di chuyen sang trai
							}
							else if (obj->getObjState() == ES_MOVE_SHELL_LEFT || obj->getObjState() == ES_MOVE_SHELL_RIGHT)
							{
								if (isSmall == true)	//mario nho
								{
									//game_Mario->num_Life--;
									//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
									//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
									changeState(MS_DIE);	//mario chet
								}
								else					//mario lon
								{
									isSmall = true;
									if (isShoot == true)	//co bullet
									{
										changeState(MS_BULLETTOSMALL);
									} 
									else
									{
										changeState(MS_BIGTOSMALL);
									}
									isShoot = false;
								}
							}

						} 
						else
						{
							obj->changeState(ES_FALL);
						}
					}
					if (obj->getIDObjType() == 9)	//goomba
					{
						if (isHasStar == false)	//chua co an sao
						{
							if (obj->getObjState() == ES_ACTIVING)
							{
								if (isSmall == true)	//mario nho
								{
									//game_Mario->num_Life--;
									//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
									//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
									changeState(MS_DIE);
								}
								else					//mario lon
								{
									isSmall = true;
									if (isShoot == true)	//co bullet
									{
										changeState(MS_BULLETTOSMALL);
									} 
									else
									{
										changeState(MS_BIGTOSMALL);
									}
									isShoot = false;
								}
							}
						} 
						else
						{
							obj->changeState(ES_FALL);
						}
					}
					if (obj->getObjectType() == OBJECT_LAND)
					{
						_x = _xPre + _vx * collision_time + 1.0f/2.0f * _accelX * collision_time *collision_time;
						_accelX = 0;
						_vx = 0;
					}

				} 
				else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
				{
					/*
					 *	Xet va cham voi Koopa
					 */
					if (obj->getIDObjType() == 12)
					{						
						if (isHasStar == false)	//chua co an sao
						{
							if (obj->getObjState() == ES_STOP)
							{
								obj->changeState(ES_MOVE_SHELL_RIGHT);	//di chuyen sang phai
							}
							else if (obj->getObjState() == ES_MOVE_SHELL_LEFT || obj->getObjState() == ES_MOVE_SHELL_RIGHT)
							{
								if (isSmall == true)	//mario nho
								{
									//game_Mario->num_Life--;
									//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
									//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
									changeState(MS_DIE);	//mario chet
								}
								else					//mario lon
								{
									isSmall = true;
									if (isShoot == true)	//co bullet
									{
										changeState(MS_BULLETTOSMALL);
									} 
									else
									{
										changeState(MS_BIGTOSMALL);
									}
									isShoot = false;
								}
							}
						} 
						else
						{
							obj->changeState(ES_FALL);
						}
					}

					if (obj->getIDObjType() == 9)	//goomba
					{
						if (isHasStar == false)	//chua co an sao
						{
							if (obj->getObjState() == ES_ACTIVING)
							{
								if (isSmall == true)	//mario nho
								{
									//game_Mario->num_Life--;
									//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
									//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
									changeState(MS_DIE);
								}
								else					//mario lon
								{
									isSmall = true;
									if (isShoot == true)	//co bullet
									{
										changeState(MS_BULLETTOSMALL);
									} 
									else
									{
										changeState(MS_BIGTOSMALL);
									}
									isShoot = false;
								}
							}
						} 
						else
						{
							obj->changeState(ES_FALL);
						}
					}

					if (obj->getObjectType() == OBJECT_LAND)
					{
						_x = _xPre + _vx * collision_time + 1.0f/2.0f * _accelX * collision_time *collision_time;
						_accelX = 0;
						_vx = 0;
					}

				}
				else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
				{
					//Xet cac obj LAND
					if (obj->getObjectType() == OBJECT_LAND)
					{

						switch (obj->getIDObjType())
						{
						case 1:	//gach binh thuong
							if (obj->getObjState() == BS_NORMAL) //trang thai gach bt
							{
								if (this->isSmall == true)		//con nho
								{
									obj->changeState(BS_MOVEUP);
									//am thanh
									//game_Mario->dxAudio->PlaySound(game_Mario->bumpSound);
								}
								else
								{
									obj->changeState(BS_BREAKING);
									//am thanh
									//tang diem
									//game_Mario->dxAudio->PlaySoundW(game_Mario->brickBreakSound);
									//game_Mario->InfoScore(50, obj->getPosX(), obj->getPosY() - 40);
									//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);
								}
							}
							break;
						case 6:	//gach dau hoi
							if (obj->getObjState() == BS_NORMAL) //trang thai gach bt
							{
								obj->changeState(BS_MOVEUP);
								/*game_Mario->dxAudio->PlaySoundW(game_Mario->brickBreakSound);
								game_Mario->InfoScore(50, obj->getPosX(), obj->getPosY() - 40);
								game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);*/
							}
							break;
						case 100:	//gach an
							if (obj->getObjState() == BS_NORMAL) //trang thai gach bt
							{
								obj->changeState(BS_MOVEUP);
							}
							break;
						case 200:	//gach chua 5 coin
							if (obj->getObjState() == BS_NORMAL) //trang thai gach bt
							{
								obj->changeState(BS_MOVEUP);
							}
							break;
						case 300:	//gach cua sao
							if (obj->getObjState() == BS_NORMAL) //trang thai gach bt
							{
								obj->changeState(BS_MOVEUP);
							}
							break;

						}
						
						_y = _yPre + _vy * collision_time + 1.0f/2.0f * _accelY * collision_time *collision_time;
						_vy = 0;
						_accelY = 0;
					}

				}
				else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
				{
					/*
					*	Va cham voi Goomba, Koopa tu tren xuong
					*/
					switch (obj->getIDObjType())
					{
					case 9:	//Goomba
						if (isHasStar == false)	//chua an sao
						{
							if (obj->getObjState() == ES_ACTIVING)
							{
								//game_Mario->InfoScore(100, obj->getPosX(), obj->getPosY() + 40);
								//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);
								//game_Mario->dxAudio->PlaySound(game_Mario->mipSound);
								obj->changeState(ES_DIED);

								_vy = 1.0; //giam enemy rui nhay len
							}
						}
						else
						{
							obj->changeState(ES_FALL);
						}
						break;

					case 12:	//Koopba
						if (isHasStar == false)	//chua an sao
						{
							if (obj->getObjState() == ES_ACTIVING)
							{
								//game_Mario->InfoScore(100, obj->getPosX(), obj->getPosY() + 40);
								//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);
								//game_Mario->dxAudio->PlaySound(game_Mario->mipSound);
								obj->changeState(ES_STOP);	//con Rua dung lai

								_vy = 1.0; //giam enemy rui nhay len
							}
							else if (obj->getObjState() == ES_MOVE_SHELL_LEFT || obj->getObjState() == ES_MOVE_SHELL_RIGHT)
							{
								obj->changeState(ES_STOP);
								this->_vy = 1.0;
							}
						}
						else	//da an sao
						{
							obj->changeState(ES_FALL);
						}
						break;
					}
					
					/*
					*	Kiem tra va cham voi cac gach, dat,...
					*/
					if (obj->getObjectType() == OBJECT_LAND)
					{
						_y = _yPre + _vy * collision_time + 1.0f/2.0f * _accelY * collision_time *collision_time + 0.1;
						_isJumping = false;
						_vy = 0;
					}
					
				}
				//Ket thuc xet va cham theo huong

				
				/*
				*	va cham voi pirhana, coin, checkpoint
				*	goomba, koopba
				*/
				switch (obj->getIDObjType())
				{
				case 11:	//Cay an thit, pirhana
					if (obj->getObjState() != IS_REMOVE)
					{
						if (isHasStar == false)		//chua an sao
						{
							if (isSmall == true)	//mario nho
							{
								changeState(MS_DIE);
								//obj->changeState(ES_REMOVE);
							} 
							else					//mario lon
							{
								isSmall = true;
								if (isShoot == true)
								{
									changeState(MS_BULLETTOSMALL);
								} 
								else
								{
									changeState(MS_BIGTOSMALL);
								}
								isShoot = false;
								//obj->changeState(ES_REMOVE);
							}
						} 
						else		//da an sao
						{
							obj->changeState(ES_REMOVE);
						}
					}
					break;

				case 14:	//coin hien
					if (obj->getObjState() == IS_IDLE)
					{
						//game_Mario->num_Coin++;
						//game_Mario->scoreArr[COIN]->Update(game_Mario->num_Coin);
						//game_Mario->InfoScore(200, obj->getPosX(), obj->getPosY() + 40);
						//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);

						//game_Mario->dxAudio->PlaySoundW(game_Mario->coinSound);
						obj->changeState(IS_REMOVE);
					}
					break;

				case 51:
					//save map
					//chuyen map, state
					//game_Mario->ChangeState(GS_NEXT_STAGE);
					break;

				case 16:	//va cham an nam mang: moving 
					//case 431:	//va cham an nam mang: moving 
					//case 432:	//va cham an nam mang: moving 
					if (obj->getObjState() == IS_MOVING)
					{
						//game_Mario->num_Life++;	//tang 1 mang
						//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
						//game_Mario->InfoScore(1000, obj->getPosX(), obj->getPosY() + 40);	//an nam tang 1000 diem
						//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);
						//game_Mario->dxAudio->PlaySound(game_Mario->bonusRiseSound);
						obj->changeState(IS_REMOVE);
					}
					break;

				case 18:	//va cham an sao: moving 
					if (obj->getObjState() == IS_MOVING)
					{
						//game_Mario->num_Life++;	//tang 1 mang
						//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
						//game_Mario->dxAudio->PlaySound(game_Mario->bonusRiseSound);
						obj->changeState(IS_REMOVE);
						this->isHasStar = true;
						this->changeState(MS_HASSTAR);
					}
					break;
				case 15:
				case 17:	//va cham an nam : moving, hoa:ready
					//case 491:	//va cham an nam : moving, hoa:ready
					//case 492:	//va cham an nam : moving, hoa:ready
					//case 50:	//va cham an nam : moving, hoa:ready
					if (obj->getObjState() == IS_MOVING || obj->getObjState() == IS_READY)
					{
						//game_Mario->InfoScore(1000, obj->getPosX(), obj->getPosY() + 40);	//an nam tang 1000 diem
						//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);
						//game_Mario->dxAudio->PlaySound(game_Mario->bonusRiseSound);
						obj->changeState(IS_REMOVE);
						if (this->isSmall == true)
						{
							this->isSmall = false;
							this->changeState(MS_SMALLTOBIG);
						}
						else
						{
							this->changeState(MS_BIGTOBULLET);
							this->isShoot = true;
						}
					}
					break;

				case 9:	//goomba
					if (isHasStar == false)	//chua co an sao
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							if (isSmall == true)	//mario nho
							{
								//game_Mario->num_Life--;
								//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
								//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
								changeState(MS_DIE);
							}
							else					//mario lon
							{
								isSmall = true;
								if (isShoot == true)	//co bullet
								{
									changeState(MS_BULLETTOSMALL);
								} 
								else
								{
									changeState(MS_BIGTOSMALL);
								}
								isShoot = false;
							}
						}
					} 
					else
					{
						obj->changeState(ES_FALL);
					}
					break;

				case 12:	//koopba
						/*
						 *	Xet va cham voi Koopa
						 */					
						if (isHasStar == false)	//chua co an sao
						{
							if (obj->getObjState() == ES_ACTIVING)
							{
								if (isSmall == true)	//mario nho
								{
									//game_Mario->num_Life--;
									//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
									//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
									changeState(MS_DIE);	//mario chet
								}
								else					//mario lon
								{
									isSmall = true;
									if (isShoot == true)	//co bullet
									{
										changeState(MS_BULLETTOSMALL);
									} 
									else
									{
										changeState(MS_BIGTOSMALL);
									}
									isShoot = false;
								}
							}
							
						} 
						else
						{
							if (obj->getObjState() != ES_FALL)
							{
								obj->changeState(ES_FALL);
							}
						}
					break;

				}

				if (this->_y - this->_sprite->getTexture()->getHeight() <= obj->getPosY() + 1.0)
				{
					isGravity = false;
				}
				else
				{
					isGravity = true;
				}


			}//Ket thuc co va cham




		}//Ket thuc co the co va cham
		else
		{

			if (this->_y - this->_sprite->getTexture()->getHeight() <= obj->getPosY() + 1.5)
			{
				isGravity = false;
			}
			else
			{
				isGravity = true;
			}

		}




		//}


		//kiem tra dong tien da duoc an hay chua? active
		if (obj->getIDObjType() == 14 || obj->getIDObjType() == 39)
		{
			if (obj->getObjState() == IS_ACTIVING && obj->getIsChangeState() == true)
			{				
				//game_Mario->num_Coin++;
				//game_Mario->scoreArr[COIN]->Update(game_Mario->num_Coin);
				//game_Mario->InfoScore(200, obj->getPosX(), obj->getPosY() - 40);
				//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);

				//game_Mario->dxAudio->PlaySoundW(game_Mario->coinSound);

				obj->setIsChangeState(false);
			}
		}
		if (obj->getIDObjType() == 17)	//nam to, hoa
		{
			if (obj->_isActiveMushRoom == true)
			{
				if (isSmall == true)
				{
					obj->changeState(IS_ACTIVING);
				}
				else
				{
					obj->changeState(IS_FLOWER);
				}

				obj->_isActiveMushRoom = false;
			}
		}

}


	//Va cham Thong Thuong

	for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
	{
		obj = *i_obj;


		if (obj->getObjectType() == OBJECT_ENEMY || obj->getObjectType() == OBJECT_ITEM)		
		{ 
			Box block(this->_xPre,
				this->_yPre - this->_height, 
				this->_width, 
				this->_height
				);


			Box box(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth(),
				obj->getHeight()				
				);


			if (Collision::RectVsRect(box, block))
			//if (Collision::_AABBCheck(block, box))
			{

				switch (obj->getIDObjType())
				{
				case 11:	//Cay an thit, pirhana
					if (obj->getObjState() != IS_REMOVE)
					{
						if (isHasStar == false)		//chua an sao
						{
							if (isSmall == true)	//mario nho
							{
								changeState(MS_DIE);
								//obj->changeState(ES_REMOVE);
							} 
							else					//mario lon
							{
								isSmall = true;
								if (isShoot == true)
								{
									changeState(MS_BULLETTOSMALL);
								} 
								else
								{
									changeState(MS_BIGTOSMALL);
								}
								isShoot = false;
								//obj->changeState(ES_REMOVE);
							}
						} 
						else		//da an sao
						{
							obj->changeState(ES_REMOVE);
						}
					}
					break;

				case 16:	//va cham an nam mang: moving 
					//case 431:	//va cham an nam mang: moving 
					//case 432:	//va cham an nam mang: moving 
					if (obj->getObjState() == IS_MOVING)
					{
						//game_Mario->num_Life++;	//tang 1 mang
						//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
						//game_Mario->InfoScore(1000, obj->getPosX(), obj->getPosY() + 40);	//an nam tang 1000 diem
						//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);
						//game_Mario->dxAudio->PlaySound(game_Mario->bonusRiseSound);
						obj->changeState(IS_REMOVE);
					}
					break;

				case 18:	//va cham an sao: moving 
					if (obj->getObjState() == IS_MOVING)
					{
						//game_Mario->num_Life++;	//tang 1 mang
						//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
						//game_Mario->dxAudio->PlaySound(game_Mario->bonusRiseSound);
						obj->changeState(IS_REMOVE);
						this->isHasStar = true;
						this->changeState(MS_HASSTAR);
					}
					break;

				case 17:	//va cham an nam : moving, hoa:ready
					//case 491:	//va cham an nam : moving, hoa:ready
					//case 492:	//va cham an nam : moving, hoa:ready
					//case 50:	//va cham an nam : moving, hoa:ready
					if (obj->getObjState() == IS_MOVING || obj->getObjState() == IS_READY)
					{
						//game_Mario->InfoScore(1000, obj->getPosX(), obj->getPosY() + 40);	//an nam tang 1000 diem
						//game_Mario->scoreArr[SCORE]->Update(game_Mario->num_Score);
						//game_Mario->dxAudio->PlaySound(game_Mario->bonusRiseSound);
						obj->changeState(IS_REMOVE);
						if (this->isSmall == true)
						{
							this->isSmall = false;
							this->changeState(MS_SMALLTOBIG);
						}
						else
						{
							this->changeState(MS_BIGTOBULLET);
							this->isShoot = true;
						}
					}
					break;


				case 9:	//goomba						
					if (isHasStar == false)	//chua co an sao
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							if (isSmall == true)	//mario nho
							{
								//game_Mario->num_Life--;
								//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
								//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
								changeState(MS_DIE);
							}
							else					//mario lon
							{
								isSmall = true;
								if (isShoot == true)	//co bullet
								{
									changeState(MS_BULLETTOSMALL);
								} 
								else
								{
									changeState(MS_BIGTOSMALL);
								}
								isShoot = false;
							}
						}
					} 
					else
					{
						obj->changeState(ES_FALL);
					}
					break;

				case 12:	//Koopa
					if (isHasStar == false)	//chua co an sao
					{
						if (obj->getObjState() == ES_ACTIVING)
						{
							if (isSmall == true)	//mario nho
							{
								//game_Mario->num_Life--;
								//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
								//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
								changeState(MS_DIE);	//mario chet
							}
							else					//mario lon
							{
								isSmall = true;
								if (isShoot == true)	//co bullet
								{
									changeState(MS_BULLETTOSMALL);
								} 
								else
								{
									changeState(MS_BIGTOSMALL);
								}
								isShoot = false;
							}
						}
						else if (obj->getObjState() == ES_MOVE_SHELL_LEFT || obj->getObjState() == ES_MOVE_SHELL_RIGHT)
						{
							if (isSmall == true)	//mario nho
							{
								//game_Mario->num_Life--;
								//game_Mario->scoreArr[NUMBER_LIFE]->Update(game_Mario->num_Life);
								//game_Mario->dxAudio->PlaySound(game_Mario->deathSound);
								changeState(MS_DIE);	//mario chet
							}
							else					//mario lon
							{
								isSmall = true;
								if (isShoot == true)	//co bullet
								{
									changeState(MS_BULLETTOSMALL);
								} 
								else
								{
									changeState(MS_BIGTOSMALL);
								}
								isShoot = false;
							}
						}
					} 
					else
					{
						if (obj->getObjState() != ES_FALL)
						{
							obj->changeState(ES_FALL);
						}

					}

					break;

				}

			}

		}

	}
	//Ket thuc va cham thong thuong 

	_xPre = _x;
	_yPre = _y;

	//Ngan mario rot xuong ma van con nhay duoc
	if (_vy < 0)
	{
		_isJumping = true;
	}
	//chuyen dong cham lai do k nhan phim
	if (_vx < 0)
	{
		_accelX = ACCEL_X / 2;
	} 
	else if (_vx > 0)
	{
		_accelX = -ACCEL_X / 2;
	}
	else
	{
		_accelX = 0;
	}
	//tao chuyen dong dung lai
	if (_accelX * _vx < 0)
	{
		if (_vx > -_accelX*3 && _vx < _accelX*3)
		{
			_vx = 0;
			_accelX = 0;
		}
	}

}


void Mario::TurnLeft()
{
	_turnLeft = true;
	if (_isJumping == true)
	{
		if (isSmall == true)
		{
			_sprite->SetFrame(8, 8);
		}
		else
		{
			_sprite->SetFrame(9, 9);
		}
	}
	else
	{
		if (isSmall == true)
		{
			_sprite->SetFrame(9, 11);
		} 
		else
		{
			_sprite->SetFrame(6, 9);
		}
	}
	//_vx = - MARIO_VX;
	_accelX = - ACCEL_X * 2;
	
	
}


void Mario::TurnRight()
{
	_turnLeft = false;
	if (_isJumping == true)
	{
		if (isSmall == true)
		{
			_sprite->SetFrame(2, 2);
		} 
		else
		{
			_sprite->SetFrame(0, 1);
		}
	}
	else
	{
		if (isSmall == true)
		{
			_sprite->SetFrame(0, 2);
		} 
		else
		{
			_sprite->SetFrame(1, 4);
		}
	}
	_accelX = ACCEL_X * 2;
}

void Mario::Jump()
{
	if(!_isJumping)
	{
		_isJumping = true;
		if (_turnLeft == true)
		{
			if (isSmall == true)
			{
				_sprite->SetFrame(8, 8);
			} 
			else
			{
				_sprite->SetFrame(9, 9);
			}
		}
		else
		{
			if (isSmall == true)
			{
				_sprite->SetFrame(2, 2);
			} 
			else
			{
				_sprite->SetFrame(0, 1);
			}
		}
		_vy = MARIO_VY;
	}
}


void Mario::Reset()
{
	if (_turnLeft == true)
	{
		if (_isJumping == true)
		{
			if (isSmall == true)
			{
				_sprite->SetFrame(8, 8);
			} 
			else
			{
				_sprite->SetFrame(9, 9);
			}
		}
		else 
		{
			if (isSmall == true)
			{
				_sprite->SetFrame(10, 11);
			} 
			else
			{
				_sprite->SetFrame(8, 9);
			}

		}
	}
	else
	{
		if (_isJumping == true)
		{
			if (isSmall == true)
			{
				_sprite->SetFrame(2, 2);
			} 
			else
			{
				_sprite->SetFrame(0, 1);
			}
		}
		else
		{
			if (isSmall == true)
			{
				_sprite->SetFrame(0, 1);
			} 
			else
			{
				_sprite->SetFrame(1, 2);
			}
		}
	}
		 
}

D3DXVECTOR2 Mario::GetPosition()
{
	D3DXVECTOR2 position;
	position.x = _x;
	position.y = _y;
	return position;
}

OBJECT_TYPE Mario::getObjectType()
{
	return OBJECT_MARIO;
}

Box Mario::getBox(float time)
{
	Box box(this->_xPre,
			this->_yPre - this->_height, 
			_width, 
			_height, 
			_vx * time + 1.0f/2.0f * _accelX * time * time, 
			_vy * time  + 1.0f/2.0f * _accelY * time * time);
	return box;
}

void Mario::changeState(int state)
{
	_state = state;
	switch (_state)
	{
	case MS_NORMAL:

		break;

	case MS_DIE:
		if (_turnLeft == true)
		{
			_sprite->SetFrame(4, 4);
		}
		else
		{
			_sprite->SetFrame(4, 4);
		}
		_vy = 1.0;
		_accelY = MARIO_GRAVITY;
		break;

	case MS_REMOVE:
		break;

	case MS_SMALLTOBIG:
		timeTransferState = 0;
		countFrame = 0;
		break;

	case MS_BIGTOSMALL:
		timeTransferState = 0;
		countFrame = 0;
		break;

	case MS_BIGTOBULLET:
		timeTransferState = 0;
		countFrame = 0;
		break;

	case MS_BULLETTOSMALL:
		timeTransferState = 0;
		countFrame = 0;
		break;

	case MS_PROTECT:
		timeProtect = 0;
		countFrame = 0;
		break;

	case MS_HASSTAR:
		timeProtect = 0;
		countFrame = 0;
		break;

	}
}

void Mario::Shoot()
{
	
	if (timeBullet > 500 && isShoot == true)
		{

			if (_turnLeft == true)
			{
				bullet = new Bullet(this->getPosX(), this->getPosY() - _sprite->getTexture()->getHeight()/2, true);
				bullet2 = new Bullet(this->getPosX() + _sprite->getTexture()->getWidth()/2, this->getPosY() - _sprite->getTexture()->getHeight()/2, true);
			}
			else
			{

				bullet = new Bullet(this->getPosX(), this->getPosY() - _sprite->getTexture()->getHeight()/2, false);
				bullet2 = new Bullet(this->getPosX() + _sprite->getTexture()->getWidth()/2, this->getPosY() - _sprite->getTexture()->getHeight()/2, false);
			}
			trace(L"Ban dang nhan phim E");
			timeBullet = 0;
		}
}